import {fileURLToPath, URL} from 'node:url'
import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import vueDevTools from 'vite-plugin-vue-devtools'
import {ElementPlusResolver} from "unplugin-vue-components/resolvers";

// 使用函数形式的 defineConfig，接收 mode 参数
export default defineConfig(({mode}) => {
    // 加载环境变量，指定为 dev 模式
    const env = loadEnv(mode, process.cwd(), '')

    return {
        base: './',
        plugins: [
            vue(),
            vueDevTools(),
            AutoImport({
                resolvers: [ElementPlusResolver()],
            }),
            Components({
                resolvers: [ElementPlusResolver()],
            }),
        ],
        resolve: {
            alias: {
                '@': fileURLToPath(new URL('./src', import.meta.url))
            },
        },
        server: {
            proxy: {
                '/api': {
                    target: env.VITE_API_URL,
                    changeOrigin: true,
                    // 可选：如果需要重写路径
                    // rewrite: (path) => path.replace(/^\/api/, '')
                }
            }
        }
    }
})